import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

const whiteList = ['/login', '404']

router.beforeEach(async(to, form, next) => {
  document.title = '人力资源管理系统-' + to.meta.title
  NProgress.start()
  // 有token
  if (store.state.user.token) {
    // 除了登录页，其他都可以跳转
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      if (!store.state.user.userInfo.userId) {
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
    // 没token
  } else {
    // 如果跳转白名单，直接放行，其他调转登录页
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
